﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using IHISCETeim.Library;

namespace IHISCETeim.Web.Export.AgentSys
{
    public partial class AgentQlty : ExportPageBase
    {
        protected override string xlsTemplateFileName
        {
            get
            {
                return "/Agent/品情汇总列表.xls";
            }
        }
        protected override string SheetName
        {
            get
            {
                return "sheet1";
            }

        }
      
        /// <summary>
        /// 查找条件
        /// </summary>
        protected string sqlwhere
        {
            get {
                return GetRequestValue<string>("q", "");
            }
        }
        protected override string SQLSort
        {
            get
            {
                return " QltyId ASC ";
            }
        }

        private DataTableCollection _dts = null;
        protected DataTableCollection dts
        {
            get
            {
                if (_dts == null)
                {
                    int TotalCount = 0;
                    _dts = AgentQltyController.GetPagination(sqlwhere,1, 999999,SQLSort, ref TotalCount);
                }
                return _dts;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write(sqlwhere);
            //return;
            InitializeWorkbook();
            ISheet sheet1 = hssfworkbook.GetSheet(SheetName);
            //
            DataTable dtList = dts[0];

            int rowindexBase = 2;
            int rowindex = 0;
            int rowReal = rowindex + rowindexBase;


            foreach (DataRow listDTDR in dtList.Rows)
            {
                int cellReal = 0;
                HSSFRow writerow = (HSSFRow)sheet1.CreateRow(rowReal);
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ClaimsNumber"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(Convert.ToDateTime(listDTDR["IssueDate"]).ToString("yyyy-MM-dd HH:mm"));
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ReportType"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["AgentName"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["MachineType"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["SerialNumber"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["CustomerName"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["IntoDate"] is DBNull?"": Convert.ToDateTime(listDTDR["IntoDate"]).ToString("yyyy-MM-dd HH:mm"));
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["IntoAddress"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["FaultDate"] is DBNull ? "" : Convert.ToDateTime(listDTDR["FaultDate"]).ToString("yyyy-MM-dd HH:mm"));
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["WorkHour"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ReportUser"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ReportTel"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["DamagePartsNumber"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["MacNature"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["IntoConditi"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["PartsRequestType_Show"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(StringHelper.StripTags(listDTDR["FaultDescription"].ToString(),false));
                writerow.CreateCell(cellReal++).SetCellValue(StringHelper.StripTags(listDTDR["FaultReason"].ToString(), false));
                writerow.CreateCell(cellReal++).SetCellValue(StringHelper.StripTags(listDTDR["ProcessMode"].ToString(), false));
                rowReal++;
            }
           
            //writerowSum.CreateCell(2).SetCellFormula(string.Format("sum(C3:C{0})", rowReal));          

            //
            WriteToFile();
        }

        protected override string GetXlsFileName(string oldFileName)
        {
            //return base.GetXlsFileName(oldFileName);

            return string.Format("{0}{1}.xls", DateTime.Now.ToString("yyyy-MM-dd"), "品情汇总列表").Trim();
        }
    }
}